﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Results;
using FatServer.Hubs;
using Microsoft.AspNet.SignalR;
using Newtonsoft.Json.Linq;

namespace FatServer.Controllers
{
    public class TriggerController : ApiController
    {
        public HttpResponseMessage Get(int id = 0)
        {
            Trigger("hello, world");
            return new HttpResponseMessage(HttpStatusCode.OK);
        }

        public HttpResponseMessage Post(JObject obj)
        {
            Trigger(obj.GetValue("value").ToString());
            return new HttpResponseMessage(HttpStatusCode.OK);
        }

        private void Trigger(string message)
        {
            var hub = GlobalHost.ConnectionManager.GetHubContext<MessageHub>();

            if (hub != null)
            {
                hub.Clients.All.onSend(message);
            }
        }
    }
}
